package com.shangxia.dreaming.common.util.txtUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shangxia.dreaming.business.pojo.TempData;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import static com.shangxia.dreaming.common.content.ConditionContent.*;

/**
 * @ProjectName: dreaming
 * @ClassName: ReadTXT
 * @Time: 2020/10/5
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class ReadTXT {

    public static List<List<Integer>> readList_List_int() throws IOException {
        List lists = new ArrayList();
        BufferedReader br = new BufferedReader(new FileReader(new File(PATH + "真号.txt")));
        String str;
        while ((str = br.readLine()) != null) {
            str.trim();
            List<Integer> list = new ArrayList();
            Arrays.stream(str.split(" ")).forEach(e -> list.add(Integer.parseInt(e)));
            lists.add(list);
        }
        br.close();
        return lists;
    }

    public static List<List<Integer>> readList_List_int(String fileName) throws IOException {
        List lists = new ArrayList();
        File file = new File(PATH + fileName + ".txt");
        if (!file.exists()){
            return lists;
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str;
        while ((str = br.readLine()) != null) {
            str.trim();
            List<Integer> list = new ArrayList();
            Arrays.stream(str.split(" ")).forEach(e -> list.add(Integer.parseInt(e)));
            lists.add(list);
        }
        br.close();
        return lists;
    }

    public static List<List<Integer>> readList_List_int(int index1, int index2) throws IOException {
        List lists = new ArrayList();
        BufferedReader br = new BufferedReader(new FileReader(new File(PATH + "真号.txt")));
        String str;
        while ((str = br.readLine()) != null) {
            str.trim();
            List<Integer> list = new ArrayList();
            String[] strs = str.split(" ");
            list.add(Integer.parseInt(strs[index1 - 1]));
            list.add(Integer.parseInt(strs[index2 - 1]));
            lists.add(list);
        }
        br.close();
        return lists;
    }

    public static Map<Integer, List<TempData>> readMap_int_TempDataList(String fileName) throws IOException {
        Map<Integer, List<TempData>> map = new HashMap<>();
        File file = new File(PATH_Result + fileName + ".txt");
        if (!file.exists()) {
            return map;
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str;
        while ((str = br.readLine()) != null) {
            str.trim();
            if (str.length() == 0) {
                continue;
            }
            JSONObject jsonObject = JSONObject.parseObject(str);
            Integer key = (Integer) jsonObject.get("key");
            List<TempData> value = JSONArray.parseArray(jsonObject.get("value").toString(), TempData.class);
            map.put(key, value);
        }
        br.close();
        return map;
    }

    public static Integer readRow(String fileName) throws IOException {
        File file = new File(PATH_Result + fileName + ".txt");
        if (!file.exists()) {
            return 0;
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str;
        while ((str = br.readLine()) != null) {
            return Integer.parseInt(str.trim());
        }
        return 0;
    }

    public static List<TempData> readTempDataList(String fileName) throws IOException {
        List<TempData> lists = new ArrayList<>();
        File file = new File(PATH_Result + fileName + ".txt");
        if (!file.exists()) {
            return new ArrayList<>();
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str;
        while ((str = br.readLine()) != null) {
            str.trim();
            if (str.length() == 0) {
                continue;
            }
            TempData dataMap = JSONObject.parseObject(str, TempData.class);
            lists.add(dataMap);
        }
        br.close();
        return lists;
    }

    public static List<String> readStrList(String fileName) throws IOException {
        File file = new File(PATH_Result + fileName + ".txt");
        if (!file.exists()) {
            return new ArrayList<>();
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = br.readLine()) != null) {
            str.trim();
            sb.append(str);
        }
        return JSONObject.parseArray(sb.toString(), String.class);

    }

}
